package com.yanqu.road.server.gameplayer.module.cricket.cmd;

import com.yanqu.road.entity.cricket.CricketMatchData;
import com.yanqu.road.entity.cricket.UserCricketData;
import com.yanqu.road.entity.cricket.court.CricketCourtBattleData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.pb.CricketPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_CRICKET_MATCH_INFO, desc = "匹配信息")
public class CricketMatchInfoCmd extends AbstractCommand {


    private CricketProto.CricketMatchInfoReqMsg reqMsg;

    private CricketProto.CricketMatchInfoRespMsg.Builder respMsgBuilder = CricketProto.CricketMatchInfoRespMsg.newBuilder();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        this.reqMsg = CricketProto.CricketMatchInfoReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.respMsgBuilder.setRet(0);
        CricketModule cricketModule = player.getModule(CricketModule.class);
        UserCricketData userCricketData = cricketModule.getUserCricketData();
        CricketMatchData cricketMatchData = userCricketData.getCricketMatchData();
        if (cricketMatchData != null && cricketMatchData.getTargetDefenseData() != null && cricketMatchData.getTargetDefenseData().getBattleDataList().size() > 0) {
            for (CricketCourtBattleData battleData : cricketMatchData.getTargetDefenseData().getBattleDataList()) {
                this.respMsgBuilder.addUserData(CricketPb.parseCricketUserBattleMatchDataTemp(battleData));
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        player.sendPacket(ClientProtocol.U_CRICKET_MATCH_INFO, this.respMsgBuilder);

    }
}
